<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/7/25
 * Time: 17:09
 */

namespace Weixin\Service;


use Common\Common\Service;
use Weixin\Component\Weixin\WeixinComponent;
use Weixin\Exception\WeixinException;
use Weixin\Request\Weixin\GetWeixinIdRequest;
use Weixin\Response\GetWeixinIdResponse;

class GetWeixinIdService implements Service
{
    public function run(GetWeixinIdRequest $request = null)
    {
        $openid = $request->getOpenId();
        $component = new WeixinComponent();
        $response = new GetWeixinIdResponse();
        try {
            $result = $component->getWeixinId($openid);
            $weixin_id = $result['id'];
            if (is_null($weixin_id) == true) {
                $response->setItIsNull();
            } else {
                $response->setWeixinId($weixin_id);
            }
        } catch (WeixinException $e) {
            throw new WeixinException($e->getMessage(), $e->getCode());
        }
        return $response;
    }
}